SUBNET_IP="192.168.99"

bak_file(){
   input_file=$1
   mv_flag=0
   if [ "$1" == "-mv" ];then
      mv_flag=1
	  input_file=$2
   fi
   dir=$(dirname $input_file)
   file=$(basename $input_file)
   CURR_DATE=$(date "+%Y%m%d")
   BAK_FILE=$dir/$file.$CURR_DATE
   i=0
   while [ -f $BAK_FILE ]
   do
       i=$(expr $i + 1)
       BAK_FILE=$dir/$file.$CURR_DATE.$i
   done
   SUDO_FLAG=""
   if [ ! -w $dir ];then
       SUDO_FLAG="sudo"
   fi
   
   if [ $mv_flag -eq 0 ];then
      $SUDO_FLAG cp -pf $input_file $BAK_FILE
   else
      $SUDO_FLAG mv -f $input_file $BAK_FILE
   fi

   if [ $? -ne 0 ];then
        echo "[Failed] cp $input_file $BAK_FILE"
   fi
}

check_install_net_tools(){
    ret=$(ifconfig 2>&1)
	if [ $? -ne 0 ];then
		echo "try to install net-tools ...".
		sudo apt -y install net-tools
	else
		echo "ifconfig ...ok"
	fi
}

change_to_eth0(){
	ret=$(ifconfig eth0 2>&1)
	if [ $? -ne 0 ];then
		echo "try to change netcard name to ethX(X=0,1,..) ..."
		bak_file /etc/default/grub
		sudo sed -i '/GRUB_CMDLINE_LINUX/ s/""/"net.ifnames=0 biosdevname=0"/g' /etc/default/grub
		sudo grub-mkconfig -o /boot/grub/grub.cfg
		net_cfg=$(ls /etc/netplan/0[01]-*.yaml)
		bak_file $net_cfg
		nb_net_card=$(cat $net_cfg|grep en.*:|wc -l)
		for((i=0;i<$nb_net_card;i++))
		do
			sudo sed -i "0,/en.*:/ s/en.*:/eth${i}:/" $net_cfg
		done
		sync
		sudo reboot now
	fi
	echo "eth0 ...ok"
}

config_dhcp(){
	ret=$(systemctl status isc-dhcp-server.service 2>/dev/null)
	ret=$?
	if [ $ret -eq 4 ];then #no isc-dhcp-server service
		echo "try to install isc-dhcp-server..."
		sudo apt -y install isc-dhcp-server
		bak_file /etc/default/isc-dhcp-server
		sudo sed -i '/^INTERFACESv[46]/ s/""/"eth1"/g' /etc/default/isc-dhcp-server
		bak_file -mv /etc/dhcp/dhcpd.conf
		cat<<EOF > /tmp/dhcpd.conf
option domain-name "example.org";
option domain-name-servers 8.8.8.8,114.114.114.114;
default-lease-time 600;
max-lease-time 7200;
ddns-update-style none;
subnet $SUBNET_IP.0 netmask 255.255.255.0 {
  range $SUBNET_IP.100 $$SUBNET_IP.254;
  option domain-name-servers $SUBNET_IP.1;
  option subnet-mask 255.255.255.0;
  option broadcast-address $SUBNET_IP.255;
  option routers $SUBNET_IP.1;
}
EOF
		sudo cp /tmp/dhcpd.conf /etc/dhcp/dhcpd.conf
		
		bak_file -mv /etc/dhcp/dhcpd6.conf
		cat <<EOF > /tmp/dhcpd6.conf
subnet6 2001:db8:0:1::/64 {
       # Range for clients
       range6 2001:db8:0:1::129 2001:db8:0:1::254;

       # Range for clients requesting a temporary address
       range6 2001:db8:0:1::/64 temporary;

       # Additional options
       option dhcp6.name-servers fec0:0:0:1::1;
       option dhcp6.domain-search "domain.example";

       # Prefix range for delegation to sub-routers
       prefix6 2001:db8:0:100:: 2001:db8:0:f00:: /56;

       # Example for a fixed host address
       host specialclient {
               host-identifier option dhcp6.client-id 00:01:00:01:4a:1f:ba:e3:60:b9:1f:01:23:45;
               fixed-address6 2001:db8:0:1::127;
       }
}
EOF
      sudo cp /tmp/dhcpd6.conf /etc/dhcp/dhcpd6.conf
	  sudo systemctl restart isc-dhcp-server.service
	elif [ $? -eq 0 ];then
	  echo "dhcp ...ok"
	fi
}

check_install_net_tools

change_to_eth0

config_dhcp

